package priv.yiyuan.example.composite.approvar1.handler;

import priv.yiyuan.example.composite.approvar1.ApprovalRequest;
import priv.yiyuan.example.composite.approvar1.Approver;

/**
 * 总监级处理者
 * <p>
 * 责任链模式-具体处理者
 *
 * @author YiYuan
 * @data 2025/8/29
 * @apoNote
 */
public class DirectorHandler implements ApprovalHandler {

    private ApprovalHandler nextHandler;

    private Approver directorApprover;

    public DirectorHandler(Approver directorApprover) {
        this.directorApprover = directorApprover;
    }

    @Override
    public void setNextHandler(ApprovalHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    @Override
    public void handleRequest(ApprovalRequest request) {
        System.out.println("\n责任链中的总监处理者 (" + directorApprover.getClass().getSimpleName() + ") 尝试处理请求...");

        if (request.getAmount() <= directorApprover.getApprovalLimit()) {
            directorApprover.processRequest(request);
            System.out.println("  请求被总监处理者成功处理。");
        }
        else if (nextHandler != null) {
            System.out.println("  总监处理者无法处理 (超出限额: " + directorApprover.getApprovalLimit() + "), 传递给下一个处理者。");
            nextHandler.handleRequest(request);
        }
        else {
            System.out.println("  请求未被处理, 且无后续处理者。");
        }
    }
}
